using BattleFury.Lib;
using BattleFury.Lib.Network;
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Xml.Serialization;

namespace BattleFury
{
#if WINDOWS || XBOX
    static class Program
    {
        public static string ConfigPath = "\\BattleFury.config";

        static void Main(string[] args)
        {
            try
            {
                Log.Cleanup();

                ClientConfiguration Config = LoadConfig();

                BattleFuryClient bfc = new BattleFuryClient();
                bfc.Configure(Config);

                bfc.Run();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Log.Append(e.Message);
                Log.Append(e.StackTrace);
                if (e.InnerException != null)
                {
                    Log.Append("Inner Exception");
                    Log.Append(e.InnerException.Message);
                    Log.Append(e.InnerException.StackTrace);
                }
                Process.Start("notepad.exe", Path.GetFullPath(Log.Filename));
            }
        }

        private static ClientConfiguration LoadConfig()
        {
            string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            if (!File.Exists(appPath + ConfigPath))
            {
                string msg = string.Format("Unable to find Client Configuration at {0}", ConfigPath);
                //GameData.Errors.Add(msg);
                Log.Append(msg);
                throw new Exception(msg);
            }

            XmlSerializer serializer = new XmlSerializer(typeof(ClientConfiguration));
            using (StreamReader reader = new StreamReader(appPath + ConfigPath))
            { return (ClientConfiguration)serializer.Deserialize(reader); }
        }
    }
#endif
}